//
//  IntroduceViewController.m
//  Daybook
//
//  Created by dllo on 16/5/30.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "IntroduceViewController.h"
#import "Common.h"

@interface IntroduceViewController ()<IntroduceViewDelegate>

@property (nonatomic, strong) IntroduceView *introduceView;

@end
@implementation IntroduceViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createView];
}

- (void)createView {
//    self.view.backgroundColor = [UIColor whiteColor];
    
    self.introduceView = [[IntroduceView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_introduceView];
    _introduceView.delegate = self;
    _introduceView.money = _model.money;
    _introduceView.type = _model.name;
    if ([_model.type isEqualToString:@"1"]) {
        _introduceView.category = @"收入";
    }else {
        _introduceView.category = @"支出";
    }
    _introduceView.remarks = _model.remarks;
    _introduceView.time = [[CalendarTool shareCalendarTool] detailStringFromDate:_model.time];
//    NSLog(@"picName: %@", _model.picName);
}

- (void)popViewController {
    [self.navigationController popViewControllerAnimated:YES];
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"detailViewDataChange" object:nil];
}

- (void)editButtonAction {
    UpdateViewController *upDate = [[UpdateViewController alloc] init];
    upDate.type = _model.type;
    upDate.picName = _model.picName;
    upDate.str = [NSMutableString stringWithFormat:@"%@", _model.money];
    upDate.remarks = _model.remarks;
    upDate.isEdit = YES;
    upDate.editTime = _model.time;
    [self.navigationController pushViewController:upDate animated:YES];
}

- (void)deleteButtonAction {
    RLMResults *result = [RealmTool getCurrentAccountFromUserDefaults];
    DayBook *currentDayBook = [result firstObject];
    RLMResults *accountBook = [currentDayBook.bookContent objectsWhere:@"time = %@", _model.time];
    RLMRealm *realms = [RLMRealm defaultRealm];
    Account *accountBookModel = accountBook[0];
    [realms beginWriteTransaction];
    [realms deleteObject:accountBookModel];
    [realms commitWriteTransaction];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"introduceChange" object:nil];

    [self.navigationController popToRootViewControllerAnimated:YES];
}


@end
